//
//  RecTravelNotesDetailsViewController.m
//  TravelEveryDay
//
//  Created by dlios on 15-10-19.
//  Copyright (c) 2015年 com.lanou3g. All rights reserved.
//

#import "RecTravelNotesDetailsViewController.h"
#import "RecTravelNotesDetailsHeaderModel.h"
#import "RecTravelNotesDetailsModel.h"
#import "RecTravelNotesDetailsTableViewCell.h"
#import "RecTravelNotesSectionModel.h"
#import "RecUserViewController.h"
@interface RecTravelNotesDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    int k;
}
@end

@implementation RecTravelNotesDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self setTitle:@"精彩游记"];
    [self setUpPage];
    [self getData];
    _iconArr = [[NSMutableArray alloc] init];
    _strArr = [[NSMutableArray alloc] init];
    _dataArr = [[NSMutableArray alloc] init];
    _sectionArr = [[NSMutableArray alloc] init];
    
}
#pragma mark - 页面创建
-(void)setUpPage
{
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 114) style:UITableViewStyleGrouped];
    _table.separatorColor = [UIColor clearColor];
    [self.view addSubview:_table];
    
    //头部
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 60 * HEIGHT)];
    headerView.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    _table.tableHeaderView = headerView;
    
    //地图
    _mapView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * HEIGHT)];
    [headerView addSubview:_mapView];
    
    //头像
    UIImageView *iconImage = [[UIImageView alloc] initWithFrame:CGRectMake(160 * WIDTH, 170 * HEIGHT, 60 * WIDTH, 60 * WIDTH)];
    [iconImage sd_setImageWithURL:[NSURL URLWithString:self.avatar_l] placeholderImage:PLACEHOLDERIMAGE];
    iconImage.userInteractionEnabled = YES;
    iconImage.layer.masksToBounds = YES;
    iconImage.layer.cornerRadius = 30. * WIDTH;
    [headerView addSubview:iconImage];
    
    //头像点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToUser)];
    [iconImage addGestureRecognizer:tap];
    
    //名字
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH, 230 * HEIGHT, SCREEN_WIDTH - 100 * WIDTH, 30 * HEIGHT)];
    nameLabel.text = self.name;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.alpha = 0.6;
    nameLabel.font = [UIFont systemFontOfSize:14.];
    [headerView addSubview:nameLabel];
    
    //描述
    _textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * WIDTH, 270 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 60 * HEIGHT)];
    _textLabel.alpha = 0.7;
    _textLabel.shadowColor = [UIColor lightGrayColor];
    _textLabel.shadowOffset = CGSizeMake(1.5, 1.5);
    _textLabel.font = [UIFont boldSystemFontOfSize:20.];
    _textLabel.textAlignment = NSTextAlignmentCenter;
    [_textLabel setNumberOfLines:2];
    [headerView addSubview:_textLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10 * WIDTH, 330 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 1)];
    lineView.backgroundColor = [UIColor grayColor];
    [headerView addSubview:lineView];
    
    //时间,里程,喜欢
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * WIDTH, 340 * HEIGHT, SCREEN_WIDTH - 60 * WIDTH, 30 * HEIGHT)];
    _timeLabel.font = [UIFont systemFontOfSize:14.];
    _timeLabel.textAlignment = NSTextAlignmentCenter;
    [headerView addSubview:_timeLabel];
    
    //信息
    _infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(30 * WIDTH, 370 * HEIGHT, SCREEN_WIDTH - 60 * WIDTH, 30 * HEIGHT)];
    _infoLabel.font = [UIFont systemFontOfSize:14.];
    _infoLabel.textAlignment = NSTextAlignmentCenter;
    _infoLabel.alpha = 0.6;
    [headerView addSubview:_infoLabel];
    
    _whiteView = [[UIView alloc] initWithFrame:CGRectMake(10 * WIDTH, 410 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, 190 * HEIGHT)];
    _whiteView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:_whiteView];
    
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _whiteView.frame.size.width, 3)];
    blueView.backgroundColor = [UIColor colorWithRed:62/255 green:176/255. blue:193/255. alpha:1];
    [_whiteView addSubview:blueView];
    
    k = 0;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            //航班,旅店...
            UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(200 * WIDTH * i + 20 * WIDTH, 60 * HEIGHT * j + 20 * HEIGHT, 40 * WIDTH, 40 * HEIGHT)];
            image.backgroundColor = [UIColor colorWithRed:62/255 green:176/255. blue:193/255. alpha:1];
            
            NSArray *arr = @[@"plane.png",@"house.png", @"sight.png",@"eat.png"];
            [image setImage:[UIImage imageNamed:[arr objectAtIndex:k]]];
            [_whiteView addSubview:image];
            _label = [[UILabel alloc] initWithFrame:CGRectMake(200 * WIDTH * i + 70 * WIDTH, 60 * HEIGHT * j + 20 * HEIGHT, 80 * WIDTH, 40 * HEIGHT)];
            [_label setTag:1000 + k];
            [_whiteView addSubview:_label];
            k++;
        }
    }
}
-(void)tapToUser
{
    RecUserViewController *user = [[RecUserViewController alloc] init];
    [self.navigationController pushViewController:user animated:YES];
    user.userId = self.model.userId;
    
}
#pragma mark - tableview Delegate & Datasource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecTravelNotesDetailsModel *model = [_dataArr objectAtIndex:indexPath.row];
    if (![model.photo isEqualToString:@""]) {
        
        return self.imageHeight + _textHeight + 100 * HEIGHT;
    }else{
        return _textHeight + 100 * HEIGHT;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _sectionArr.count;
}
#pragma mark -自定义section
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    RecTravelNotesSectionModel *sectionModel = [_sectionArr objectAtIndex:section];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(10 * WIDTH, 0, SCREEN_WIDTH - 20 * WIDTH, 40 * HEIGHT)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40 * HEIGHT)];
    label.textColor = [UIColor colorWithRed:243/255. green:198/255. blue:37/255. alpha:1];
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor colorWithRed:251/255. green:246/255. blue:233/255. alpha:1];
    label.text = [NSString stringWithFormat:@"第%@天  %@",sectionModel.day,sectionModel.date];
    [headerView addSubview:label];
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40 * HEIGHT;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecTravelNotesDetailsModel *model = [_dataArr objectAtIndex:indexPath.row];
    static NSString *cellID = @"cell";
    RecTravelNotesDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[RecTravelNotesDetailsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
   
        //照片自适应高度
        if (![model.photo isEqualToString:@""]) {
            self.imageHeight = 300 * HEIGHT * [[model.photo_info objectForKey:@"h"] integerValue] / [[model.photo_info objectForKey:@"w"] integerValue];
        }else{
            self.imageHeight = 0;
        }
        cell.image.frame = CGRectMake(10 * WIDTH, 0, SCREEN_WIDTH - 20 * WIDTH, self.imageHeight);
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //文字自适应高度
    _textHeight = [StringWithHeight heightWithText:model.text width:SCREEN_WIDTH - 20 * WIDTH font:[UIFont systemFontOfSize:16.]];
    cell.myTextLabel.frame = CGRectMake(10 * WIDTH, cell.image.frame.size.height + 10 * HEIGHT, SCREEN_WIDTH - 20 * WIDTH, _textHeight);
    
    [cell getData:model];
    return cell;
}
#pragma mark - 数据获取
-(void)getData
{
    [Refresh getdataWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%@/waypoints/",self.myId] block:^(id block) {
        RecTravelNotesDetailsHeaderModel *model = [RecTravelNotesDetailsHeaderModel modelWithDictionary:block];
        
        //地图图片
        [_mapView sd_setImageWithURL:[NSURL URLWithString:model.trackpoints_thumbnail_image] placeholderImage:PLACEHOLDERIMAGE];
        _textLabel.text = model.name;
        
        //时间,里程,喜欢赋值
        if (![model.mileage.stringValue isEqualToString:@"0"]) {
            
            NSString *str = [model.mileage.stringValue substringWithRange:NSMakeRange(0, 4)];
            _infoLabel.text = [NSString stringWithFormat:@"     %@天                 %@km              %@",model.day_count,str,model.recommendations];
        }else{
            _infoLabel.text = [NSString stringWithFormat:@"     %@天                 0km              %@",model.day_count,model.recommendations];
        }
        
        _timeLabel.text = [NSString stringWithFormat:@"%@      |       里程       |       喜欢",model.first_day];
        
        NSDictionary *dic = [block objectForKey:@"poi_infos_count"];
        
        //详细信息赋值
        UILabel *flightLabel = (UILabel *)[self.view viewWithTag:1000];
        flightLabel.text = [NSString stringWithFormat:@"%@航班",[[dic objectForKey:@"flight"] stringValue]];
        
        UILabel *sightLabel = (UILabel *)[self.view viewWithTag:1002];
        sightLabel.text = [NSString stringWithFormat:@"%@景点",[[dic objectForKey:@"sights"] stringValue]];
        
        UILabel *houseLabel = (UILabel *)[self.view viewWithTag:1001];
        houseLabel.text = [NSString stringWithFormat:@"%@旅店",[[dic objectForKey:@"hotel"] stringValue]];
        
        UILabel *eatLabel = (UILabel *)[self.view viewWithTag:1003];
        eatLabel.text = [NSString stringWithFormat:@"%@餐厅",[[dic objectForKey:@"restaurant"] stringValue]];
        
        //国家图标数据获取
        for (NSDictionary *dic in [block objectForKey:@"covered_countries"]) {
            NSString *icon = [dic objectForKey:@"icon"];
            [_iconArr addObject:icon];
        }
        //国家图标赋值
        for (int i = 0; i < _iconArr.count; i++) {
            
            UIImageView *countryImage = [[UIImageView alloc] initWithFrame:CGRectMake(50 * WIDTH * i + 20 * WIDTH, 130 * HEIGHT, 40 * WIDTH, 40 * WIDTH)];
            [countryImage sd_setImageWithURL:[NSURL URLWithString:[_iconArr objectAtIndex:i]] placeholderImage:PLACEHOLDERIMAGE];
            countryImage.layer.masksToBounds = YES;
            countryImage.layer.cornerRadius = 20. * WIDTH;
            countryImage.backgroundColor = [UIColor redColor];
            [_whiteView addSubview:countryImage];
            
        }
        
        //国家赋值
        for (NSString *str in [block objectForKey:@"cities"]) {
            [_strArr addObject:str];
        }
        NSString *tempStr = @"";
        UILabel *countryLbael = [[UILabel alloc] initWithFrame:CGRectMake(50 * WIDTH * _iconArr.count + 30 * WIDTH, 130 * HEIGHT, 220 * WIDTH, 40 * HEIGHT)];
        [_whiteView addSubview:countryLbael];
        for (int i = 0; i < _strArr.count; i++) {
            //拼接字符串
            tempStr = [tempStr stringByAppendingString:[NSString stringWithFormat:@"%@·",[_strArr objectAtIndex:i]]];
        }
        countryLbael.text = tempStr;
        
        for (NSDictionary *dic in [block objectForKey:@"days"]) {
            //section数据获取
            RecTravelNotesSectionModel *sectionModel = [RecTravelNotesSectionModel modelWithDictionary:dic];
            [_sectionArr addObject:sectionModel];
            
            for (NSDictionary *dataDic in [dic objectForKey:@"waypoints"]) {
                //tableview 数据获取
                RecTravelNotesDetailsModel *model = [RecTravelNotesDetailsModel modelWithDictionary:dataDic];
                [_dataArr addObject:model];
                
                if (![[dataDic objectForKey:@"photo"] isEqualToString:@""]) {
                }
            }
        }
        _table.delegate = self;
        _table.dataSource = self;
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
